package com.beautify.controller;

import com.beautify.entity.dto.LoginDto;
import com.beautify.entity.dto.RegisterDto;
import com.beautify.entity.dto.UserChangePwd;
import com.beautify.entity.pojo.Result;
import com.beautify.entity.pojo.User;
import com.beautify.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    // 登录
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto) {

        log.info("用户登录信息：{}",loginDto);
        if(loginDto.getUsername()==null || loginDto.getPassword()==null){
            return Result.error("用户名或密码错误");
        }
        return userService.login(loginDto.getUsername(),loginDto.getPassword());
    }

    // 注册
    @PostMapping("/register")
    public Result register(@RequestBody RegisterDto registerDto) {
        return userService.register(registerDto.getTypes(),registerDto.getUsername(),registerDto.getPassword());
    }

    //注销
    @DeleteMapping("/deleteuser/{userid}")
    public Result deleteUser(@PathVariable("userid") Integer userid) {
        return userService.deleteUser(userid);
    }

    //修改信息
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    //修改密码
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody UserChangePwd userChangePwd) {
        return userService.updatePassword(userChangePwd.getOldPassword(), userChangePwd.getNewPassword(), userChangePwd.getUserId());
    }

    //查询用户信息
    @GetMapping("/findById/{userid}")
    public Result findById(@PathVariable("userid") Integer userid){
       return userService.getById(userid);
    }

    @GetMapping("/blindRegister")
    public Result getDeviceIdentifier() {
        return userService.getDeviceIdentifier();
    }

    @PostMapping("/feedback")
    public Result userFeedback(@RequestBody String content) {
        log.info("{}",content);
        return Result.success();
    }

}
